package com.koron.operation.classes.schedule.bean.dto;

import com.koron.operation.classes.info.bean.po.ClassesItem;
import com.koron.operation.classes.schedule.bean.po.ClassesSchedule;
import com.koron.operation.classes.schedule.bean.po.ClassesShift;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author jzp
 * @since 2021/10/21
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class ScheduleDTO extends ClassesSchedule {

    @ApiModelProperty("班别列表")
    private List<ClassesItem> items;

    @ApiModelProperty("排班明细")
    private List<List<ClassesShiftDTO>> shiftList = new ArrayList<>();

    public void fromClassesScheduleDTO(ClassesScheduleDTO classesScheduleDTO) {
        shiftList.clear();
        BeanUtils.copyProperties(classesScheduleDTO, this);
        if (CollectionUtils.isEmpty(items)) {
            return;
        }
        int size = items.size();
        List<ClassesShiftDTO> shifts = classesScheduleDTO.getShifts();
        shifts.sort(Comparator.comparingInt(ClassesShift::getSort));
        for (int i = 0; i < shifts.size(); i = i + size) {
            if (i + size >= shifts.size()) {
                this.shiftList.add(shifts.subList(i, shifts.size()));
                break;
            }
            this.shiftList.add(shifts.subList(i, i + size));
        }
    }

    public List<ClassesShiftDTO> toClassesShiftDTOs() {
        List<ClassesShiftDTO> classesShiftDTOs = new ArrayList<>();
        shiftList.forEach(classesShiftDTOs::addAll);
        return classesShiftDTOs;
    }

}
